#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

HANDLE InterFace,GameScence1,GameScence2;
COORD coord={0,0};
DWORD bytes=0;
int data[10][10]=
	{
		{1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,2,2,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,3,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1}
	};
struct Postion
{
	x=0;
	y=0;
};
int bodyLength
struct Postion headPos=[0,0];
char moveDirection='d';
int rows=sizeof(data)/sizeof(data[0]);
int cols=sizeof(data[0])/sizeof(data[0][0]);

void Start();
void NewGame();
void MoveRight();
void MoveLeft();
void MoveUp();
void MoveDown();
void FindBody();

int main()
{
	InterFace = CreateConsoleScreenBuffer(
		GENERIC_WRITE,
		FILE_SHARE_WRITE,
		NULL,
		CONSOLE_TEXTMODE_BUFFER,
		NULL
	);
	GameScence1 = CreateConsoleScreenBuffer(
		GENERIC_WRITE,
		FILE_SHARE_WRITE,
		NULL,
		CONSOLE_TEXTMODE_BUFFER,
		NULL
	);
	GameScence2 = CreateConsoleScreenBuffer(
		GENERIC_WRITE,
		FILE_SHARE_WRITE,
		NULL,
		CONSOLE_TEXTMODE_BUFFER,
		NULL
	);
	CONSOLE_CURSOR_INFO cci;
	cci.bVisible = 0;
	cci.dwSize =1;
	SetConsoleCursorInfo(InterFace, &cci);
	SetConsoleCursorInfo(GameScence1,&cci);
	SetConsoleCursorInfo(GameScence2,&cci);

	Start();
	return 0;
}
void Start()
{
	char interfaceArray[5][36]=
	{
		"------------------------------------",
		"---          1.new game          ---",
		"---          2 history           ---",
		"---          3.exit              ---",
		"------------------------------------"
	};
	char userChoice;
	int row=sizeof(interfaceArray)/sizeof(interfaceArray[0]);
	int col=sizeof(interfaceArray[0])/sizeof(interfaceArray[0][0]);
	for(int i=0;i<row;i++)
	{
		coord.Y=i;
		WriteConsoleOutputCharacter( InterFace,interfaceArray[i],col, coord, &bytes);
	}
	SetConsoleActiveScreenBuffer(InterFace);

	userChoice=getch();
	if(userChoice=='1')
	{
		SetConsoleActiveScreenBuffer(GameScence1);
		while(1)
		{
			NewGame();
			Sleep(300);
		}
	}
	else if(userChoice=='2')
		exit(0);
	else if(userChoice=='3')
		exit(0);
	else	exit(0);
}
void NewGame()
{
	char map[rows][cols];
	for(int r=0;r<rows;r++)
	{
		for(int c=0;c<cols;c++)
		{
			if(data[r][c]==0)		map[r][c]=' ';
			else if(data[r][c]==1)	map[r][c]='*';
			else if(data[r][c]==2)	map[r][c]='o';
			else	map[r][c]='x';
		}
	}
	for(int i=0;i<cols;i++)
	{
		coord.Y=i;
		WriteConsoleOutputCharacter( GameScence1,map[i],cols, coord, &bytes);
	}
	if(kbhit())
		moveDirection=getch();
	else
	{
		
	}
}
void MoveRight()
{

}
void FindBody()
{
	for(int r=0;r<rows;r++)
		for(int c=0;c<cols;c++)
			if(data[r][c]==2)	bodyLength++;
	struct Postion body[bodyLength];
	int i=0;
	for(int r=0;r<rows;r++)
	{
		for(int c=0;c<cols;c++)
		{
			if(data[r][c]==2)
			{
				body[i].x=r;
				body[i].y=c;
				i=(i<bodyLength)?i++:i;
			}
		}
	}
}